package org.jeecg.common.util.dingtalk;

import org.jeecg.common.constant.ProcessConstant;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiDepartmentCreateRequest;
import com.dingtalk.api.request.OapiDepartmentDeleteRequest;
import com.dingtalk.api.request.OapiDepartmentListRequest;
import com.dingtalk.api.request.OapiDepartmentUpdateRequest;
import com.dingtalk.api.response.OapiDepartmentCreateResponse;
import com.dingtalk.api.response.OapiDepartmentDeleteResponse;
import com.dingtalk.api.response.OapiDepartmentListResponse;
import com.dingtalk.api.response.OapiDepartmentUpdateResponse;
import com.taobao.api.ApiException;

import me.kagura.JSONQuery;
import me.kagura.JsonResult;
import me.kagura.exception.FieldNotExistException;
import me.kagura.exception.TypeNotMismatchException;

/**
 * @description 操作钉钉部门接口
 * @author heckerstone
 * @date 2020-08-14
 */
public class DingDepartmentUtils {
	
	/**
	 * @author heckerstone
	 * @date 2020-08-14
	 * @description 获取本企业所有部门
	 */
	public static void getAllDepartments() {
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_GETALLDEPARTMENTS);
			OapiDepartmentListRequest req = new OapiDepartmentListRequest();
			req.setHttpMethod("GET");
			OapiDepartmentListResponse rsp = client.execute(req, DingTalkUtils.get_token());
			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author heckerstone
	 * @date 2020-08-14
	 * @description 创建部门
	 * @param parendDepartmentId 上级部门ID
	 * @param departmentName 部门名称
	 * @param sourceIdentifier 本地数据库与钉钉部门关联字段
	 * @return 返回钉钉部门ID
	 */
	public static String createDepartment(String parendDepartmentId,String departmentName,String sourceIdentifier) {
		String deptId="1";
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_CREATEDEPARTMENT);
			OapiDepartmentCreateRequest req = new OapiDepartmentCreateRequest();
			if(parendDepartmentId==null||parendDepartmentId.equals("")) {
				parendDepartmentId="1";
			}
			req.setParentid(parendDepartmentId);//设置上级部门
			req.setName(departmentName);//设置部门名称
			req.setSourceIdentifier(sourceIdentifier);//自建数据库关联字段
			//执行钉钉创建部门，并得到返回的部门信息
			OapiDepartmentCreateResponse rsp = client.execute(req, DingTalkUtils.get_token());
			JsonResult jsonResult = JSONQuery.select(rsp.getBody(), "id");
			deptId=jsonResult.toString();
//			System.out.println(rsp.getBody());
		} catch (ApiException e) {
		} catch (TypeNotMismatchException e) {
			e.printStackTrace();
		} catch (FieldNotExistException e) {
			e.printStackTrace();
		}
		return deptId;
	}
	
	/**
	 * 
	* @Title: updateDepartment 
	* @Description: TODO(这里用一句话描述这个方法的作用) 
	* @author heckerstone heckerstone@163.com
	* @date Aug 15, 2020 11:03:03 AM 
	* @param @param deptId
	* @param @param parentDeptId
	* @param @param departmentName
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
	public static boolean updateDepartment(String deptId,String parentDeptId,String departmentName) {
		boolean flag=false;
		try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_UPDATEDEPARTMENT);
			OapiDepartmentUpdateRequest req = new OapiDepartmentUpdateRequest();
			req.setId(Long.parseLong(deptId));
			if(parentDeptId==null||parentDeptId.equals("")) {
				parentDeptId="1";
			}
			req.setParentid(parentDeptId);
			req.setName(departmentName);
			OapiDepartmentUpdateResponse rsp = client.execute(req, "f32a5a7938353d4daf8f4e853d73b008");
			flag=true;
//			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	/**
	 * 
	* @Title: deleteDepartment 
	* @Description: 删除钉钉部门
	* @author heckerstone heckerstone@163.com
	* @date Aug 15, 2020 11:11:11 AM 
	* @param @param deptId
	* @param @return    设定文件 
	* @return boolean    返回类型 
	* @throws
	 */
    public static boolean deleteDepartment(String deptId) {
    	boolean flag=false;
    	try {
			DingTalkClient client = new DefaultDingTalkClient(ProcessConstant.URL_DELETDEPARTMENT);
			OapiDepartmentDeleteRequest req = new OapiDepartmentDeleteRequest();
			req.setId(deptId);//钉钉部门编号
			req.setHttpMethod("GET");
			OapiDepartmentDeleteResponse rsp = client.execute(req, DingTalkUtils.get_token());
			flag=true;
//			System.out.println(rsp.getBody());
		} catch (ApiException e) {
			e.printStackTrace();
		}
    	return flag;
    }

}
